

$(function(){


	//取当前控制器名称函数
	function Contr(){
			//取出当前URL
			var ContrStr = window.location.href;
			
			//切字符串
			ContrStr = ContrStr.split('/');
			//alert(ContrStr);
			//取得控制器名
			var pre = ContrStr.indexOf("admin");
			var next = ContrStr.indexOf("index");
			
			
			var controller = ContrStr[pre+1];
			//alert(controller);
			return 	controller;
		}



//监听ADDMODIFY按钮动作
$('#myCateModal .modal-footer button[name = "addmodify"]').click(function(){
	//alert("按下了增加键");
	//读取DIALOG里的input内容
	
	//取出当前控制器名称
	var controller = Contr();

	//alert(controller);
	var str = "";
	if(controller == "Cate"){
		var InputIndex = ['id','name','sort','thumb','status'];
		//var taget = "$('.modal-body input[name ='+ InputIndex[i] +']')";
	}else if(controller == "Attr"){
		var InputIndex =['id','name','color'];
	}
	Action = "add";

	if($('#myCateModal select').prop('value') == "空" ){
		var pid = "0";
	}else{
		var pid = $('#myCateModal select').prop('value');
	}
	//alert(pid);

	str = pid + ",";
	 //alert(pid);
	 for(var i = 0; i < InputIndex.length; i++){
	 	if(($('#myCateModal input[name ='+ InputIndex[i] +']').prop('value'))){
			//alert("存在");
			str += $('#myCateModal input[name ='+ InputIndex[i] +']').prop('value') + ",";
		}else{
			str += $('#myCateModal input[name ='+ InputIndex[i] +']').attr('placeholder') + ",";
		}
	}
	//alert(str);
	//末尾的逗号去掉
	if(str.substr(str.length-1) == ','){
		str = str.substr(0,str.length-1);
	}
	//掉用AJAX 函数发送
	AjaxSend(Action,str,controller);
})

//得到模态对话框的修改内容并发送
//监听SAVEMODIFY按钮动作

$('#myModal .modal-footer button[name = "savemodify"]').click(function(){
	//alert("按下了修改键");
	//读取DIALOG里的input内容	
	//取出当前控制器名称
	var controller = Contr();
	//alert(controller);
	var str = "";
	if(controller == "User"){
		var InputIndex = ['id','username','password','create_time','update_time','ip','email'];
	}else if(controller == "Cate"){
		var InputIndex = ['id','name','pid','sort','thumb','status','update_time'];
	}else if(controller == "Attr"){
		var InputIndex = ['id','name','color','update_time'];
	}

	//var taget = "$('.modal-body input[name ='+ InputIndex[i] +']')";
	Action = "modify";
	// var str = "";
	// var InputIndex = ['id','username','password','create_time','update_time','ip','email'];
	//alert(1);
	//alert(taget);
	//alert($('.modal-body input[name ="id"]').prop('value'));

	for(var i = 0; i < InputIndex.length; i++){
		if(($('#myModal .modal-body input[name ='+ InputIndex[i] +']').prop('value'))){
			//alert("存在");
			str += $('#myModal .modal-body input[name ='+ InputIndex[i] +']').prop('value') + ",";
		}else{
			str += $('#myModal .modal-body input[name ='+ InputIndex[i] +']').attr('placeholder') + ",";
		}
		//str += (taget.attr('value') ? taget.attr('value') : taget.attr('placeholder')) + ",";
	}
	//alert(str);
	//末尾的逗号去掉
	if(str.substr(str.length-1) == ','){
		str = str.substr(0,str.length-1);
	}
	//掉用AJAX 函数发送
	AjaxSend(Action,str,controller);
});
//iCheck插件的全选与全不选
$('#theadcheck').on('ifChecked ifUnchecked', function(event) {
	if (event.type == 'ifChecked') {
		$('input[name = "checkbox1"]').iCheck('check');

	} else {
            // checkboxes.iCheck('uncheck');
            $('input[name = "checkbox1"]').iCheck('uncheck');
            
        }
    });



//iCheck插件取值,发送AJAX
$('button[name ="ActionButton"]').click(function(){

    //首先取得动作框的动作
    
    var Action = $(this).siblings('select').val(); 
alert(0);
    if(Action == "修改"){
    	Action = "modify";
        alert(1);
    	//修改表格的展示与消失,增加类	
    	$(this).attr('data-target' , "#myModal");
    }/*else if(Action == "删除"){
    	Action = "delete";
    	$(this).attr('data-target' , "");
    }else if(Action == "增加"){
    	Action = "add";
    	//修改表格的展示与消失,增加类	
    	$(this).attr('data-target' , "#myCateModal");
    }else{
    	alert("请选择操作内容！！！");
    }*/

    //取出当前控制器名称
    var controller = Contr();

    //发送标识符
    var accept = "0";
    //发送字符串
    var str="";

     //alert(typeof(Action));
     /******如果是删除动作*******/
     if(Action == "delete"){

     	$("input[name='checkbox1']:checkbox").each(function(){
     		if(true == $(this).is(':checked')){
        	//弹出undefine
        	if($(this).parent().parent().siblings("td[name = 'id']").html()){

        		str += $(this).parent().parent().siblings("td[name = 'id']").html() +",";
        	}
        }
    });
    	//末尾的逗号去掉
    	if(str.substr(str.length-1) == ','){
    		str = str.substr(0,str.length-1);
    	}
    	//alert(str);
    	accept = "1";
    }
    /******如果是修改动作*******/
    /***修改动作的模态框设置开始***/
    /*if(Action == "modify"){
    	//alert(1);
    	$(".checked").parent().siblings('td').each(function(){
        		//alert(3);
        	//遍历每个TD的内容
        	str +=	$(this).not(".tdbox").html() +",";

        });*/

        //末尾的逗号去掉
        /*if(str.substr(str.length-1) == ','){
            str = str.substr(0,str.length-1);
        }*/
        //设置弹出框的内容为本字符串
        // var ModifyArray = str.split(',');
        if(controller == "User"){
        	var InputIndex = ['id','username','password','create_time','update_time','ip','email'];
        }else if(controller == "Cate"){
        	var InputIndex = ['id','name','pid','sort','thumb','status','update_time'];
        }else if(controller == "Blog"){
        	var InputIndex = ['id','title','summary','img','activity','update_time'];
        }else if(controller == "Attr"){
        	var InputIndex = ['id','name','color','update_time'];
        }
        //alert(InputIndex['2']);
        //alert(InputIndex[0]);
        for(var i = 0; i < InputIndex.length; i++){
        	
        	$('#myModal .modal-dialog .modal-body input[name = '+ InputIndex[i]+']').attr('placeholder',ModifyArray[i]);
        }
    	//accept = "1";
    	/**修改动作的模态框设置到此结束**/
    // }

    /******如果是增加动作******
   /* if(Action == "add"){
    	
    	str += $(".checked").parent().siblings('td[name="id"]').html();

    	if(controller == "Cate"){
	        //末尾的逗号去掉,得到PID
	        if(str.substr(str.length-1) == ','){
	        	str = str.substr(0,str.length-1);
	        }
        

	        var setpid = (str != "undefined")  ? str : "0";

	        if(setpid == "0" || setpid == "undefined"){
	        	var setpidText = "空";
	        }else{
	        	var setpidText = "加在id为"+setpid+"下";
	        }

    	}
    	//设置弹出的模态窗口ADD 的PID 为STR
    	// $('#myCateModal .modal-dialog .modal-body select').prepend("<option value= "+setpid+">" + setpid + "</option>");
    	$('#myCateModal .modal-dialog .modal-body select option').first().attr('value',setpid);
    	$('#myCateModal .modal-dialog .modal-body select option').first().html(setpidText);

    	
    }*/




    //检测是否有选中的内容,str不为0 ,标识符accept = 1才给发送
    /*if(str.length != 0 && accept == "1"){
    	//alert("准备发送");
    	//AjaxSend(Action,str,controller);
    	
    }*/

});



/**************本地天气模块*********************/



//AJAX发送函数
    /*function AjaxSend(Action,controller){
        //alert("发送AJAXing!!!");
        $.ajax({  

            datatype:"json",
                    type : "POST",  //提交方式  
                     url : "http://localhost/AdminFrame/public/admin/" + controller +"/"+Action+controller,//路径 
                    //url : "http://localhost/AdminFrame/public/admin/user/"+Action+"cate",//路径 
                    data : {  
                        "action":Action
                    },//数据，这里使用的是Json格式进行传输  
                    success : function(result) {//返回数据根据结果进行相应的处理
                        
                        //alert("ok");
                        //转成json 字符串
                        var ResultJsonObject = $.parseJSON(result);
                        
                       
                        
                        //var a = $('#content .weather_block > div > h3').html();
                        //alert(a);

                        var location = "<span class='fa  fa-map-marker'></span> "+ ResultJsonObject.cityname[0]+''+ResultJsonObject.cityname[1] +",中国";
                        var city = '<span class="fa  fa-map-marker"></span> '+ ResultJsonObject.cityname[0]+''+ResultJsonObject.cityname[1];
                        var now = ResultJsonObject.now[0]+'<sup>o</sup>';


                        $('#content .weather_block > div > h3').html(city);
                        $('#content .weather_block > div > h1').html(now);
                        $('#content .location > p').html(location);

                         var detail = ResultJsonObject.stateDetailed[0];
                         //alert(detail);
                         var WeatherCode = getWeatherCode(detail);

                         $('#content .weather_block .weather_block_view').html(WeatherCode);
                         //alert(a);
                        //WeatherArray['暴雨'] = ''

                        

                       
                    },
                    error : function(result){
                        //alert("获取失败");
                    } 
                }); 
    }*/

    $(document).ready(function(){

        var Action = "getInfo";
        var controller = "Weather";

        getWeather(Action,controller);
    })

     
	//alert(1);
});




